package com.hz.controller;

import com.hz.pojo.File;
import com.hz.pojo.Result;
import com.hz.pojo.User;
import com.hz.service.UserService;
import com.hz.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")//URL
    public Result login(@RequestBody User user){
        log.info("用户登录“{}",user);
        User u = userService.login(user);
        //登录成功，生成令牌并下发令牌
        if(u != null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("username",u.getUsername());
            claims.put("password",u.getPassword());
            String jwt = JwtUtils.generateJwt(claims); // jwt包含了当前登录的用户信息
            return Result.success(jwt);
        }
        //登录失败，返回错误提示信息
        return Result.error("用户名或密码错误");
    }

    @PostMapping("/modify")
    public Result modify(@RequestBody User user){
        log.info("修改用户信息");
        userService.modify(user);
        return Result.success();
    }

    @PostMapping("/feedback")
    public Result feedback(@RequestBody File file){
        log.info("用户反馈");
        userService.feedback(file);
        return Result.success();
    }
}
